console.log('tax injected');
import { insertJQuery } from '../../lib/utils.js';
import { autoInputAccount } from './option/autoInputAccount.js';
import { fillHsqj } from './option/hsqj.js';
import { fillCwbb } from './option/fillCwbb.js';
import { getNationData } from './utils.js';


(async () => {
  if (!window.jQuery) {
    console.log(123);
    await insertJQuery();
  }
})();

try {
  $(window).load(() => {
    window.postMessage(
      {
        event: 'getBaseInfo'
      },
      '*'
    );
  });
} catch (e) {
  setTimeout(() => {
    window.postMessage(
      {
        event: 'getBaseInfo'
      },
      '*'
    );
  }, 2000)
}




window.addEventListener(
  'message',
  async function ({ data }) {
    // 只监听来自于content script的信息
    if (data.sender !== 'content-script') {
      return false;
    }
    if (data.event === 'setSaasData') {
      data = data.data;
      let sheet = data.saasData.sheet;
      if (sheet === 'sds' || sheet === 'zzs') {
        // fillInput(data, sheet);
      } else if (sheet.includes('hsqj')) {
        fillHsqj(data);
        return true;
      } else {
        fillCwbb(data, sheet);
      }
    }
    if (data.event === 'getNationData') {
      getNationData(data.data.taxType);
    }

    if (data.event === 'getBaseInfo') {
      const companyInfoStr = JSON.stringify(data.data);
      // 将数据存储到localStorage(sessionStorage只能保存同一窗口,税局会打开新的窗口)
      // 先清除本地储存(避免数据受到污染)
      window.localStorage.removeItem('companyInfo');
      window.localStorage.setItem('companyInfo', companyInfoStr);
      autoInputAccount(data.data);
    }
  },
  false
);
